package cn.itsource.lookback;

/**
 * 
 *
 */
public class Student {
	private String name;
	private int age;
	private String sex;
	
	public Student() {
		super();
	}
	
	public Student(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {//Object是所有类的父类，所有可以接收任意类型的对象，这就是使用多态（向上造型）
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		/*
		 *getClass() 相当于this.getClass()就是调用equals方法，获取的对象所对应的字节码文件
		 *obj.getClass()：就是获取当前父类obj中究竟装的是哪一个子类的字节码文件
		 */
		if (getClass() != obj.getClass())
			return false;
		//以下代码就是调用子类Student中特有的变量，必须要强制转换。因为上面的代码中，都已经做好判断了，所以，直接这里强转即可
		Student other = (Student) obj;
		if (age != other.age)//other.age是在调用子类特有的成员
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "[" + name + ", " + age + ", " + sex + "]";
	}
	
}
